const FBMod = @import("framebuffer.zig");
const PSF2 = @import("font_psf2.zig");
const utf8 = @import("utf8.zig");

var psf2_font: ?PSF2.Font = null;
pub const Text = struct {
    pub fn init() void { initAscii(); }
    pub fn use_psf2(bytes: []const u8) void {
        if (PSF2.Font.initFromBytes(bytes)) |f| { psf2_font = f; }
    }
    pub fn drawChar(x: usize, y: usize, ch: u8, color: u32) void {
        const glyph = font8x16[ch];
        var row: usize = 0;
        while (row < 16) : (row += 1) {
            const bits = glyph[row];
            var col: usize = 0;
            var mask: u8 = 0x80;
            while (col < 8) : (col += 1) {
                if ((bits & mask) != 0) {
                    FBMod.FB.putPixel(x + col, y + row, color);
                }
                mask = mask >> 1;
            }
        }
    }

    pub fn drawString(x: usize, y: usize, s: []const u8, color: u32) void {
        var cx: usize = x;
        var i: usize = 0;
        if (psf2_font) |f| {
            while (i < s.len) : (i += blk.len) {
                const blk = utf8.next(s, i);
                if (blk.len == 0) break;
                const cp = blk.cp;
                f.drawGlyph(FBMod.FB.putPixel, cx, y, cp, color);
                cx += @as(usize, f.hdr.width);
            }
        } else {
            while (i < s.len) : (i += 1) {
                const b = s[i];
                if (b < 128) drawChar(cx, y, b, color);
                cx += 8;
            }
        }
    }
};

var font8x16: [128][16]u8 = undefined;
fn initAscii() void {
    var i: usize = 0;
    while (i < 128) : (i += 1) {
        var r: usize = 0;
        while (r < 16) : (r += 1) font8x16[i][r] = 0;
    }
    font8x16['A'] = [_]u8{0x18,0x24,0x42,0x42,0x7E,0x42,0x42,0x42,0,0,0,0,0,0,0,0};
    font8x16['B'] = [_]u8{0x7C,0x42,0x42,0x7C,0x42,0x42,0x42,0x7C,0,0,0,0,0,0,0,0};
    font8x16['C'] = [_]u8{0x3C,0x42,0x40,0x40,0x40,0x40,0x42,0x3C,0,0,0,0,0,0,0,0};
    font8x16['D'] = [_]u8{0x78,0x44,0x42,0x42,0x42,0x42,0x44,0x78,0,0,0,0,0,0,0,0};
    font8x16['E'] = [_]u8{0x7E,0x40,0x40,0x7C,0x40,0x40,0x40,0x7E,0,0,0,0,0,0,0,0};
    font8x16['F'] = [_]u8{0x7E,0x40,0x40,0x7C,0x40,0x40,0x40,0x40,0,0,0,0,0,0,0,0};
    font8x16['G'] = [_]u8{0x3C,0x42,0x40,0x40,0x4E,0x42,0x42,0x3C,0,0,0,0,0,0,0,0};
    font8x16['H'] = [_]u8{0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x42,0,0,0,0,0,0,0,0};
    font8x16['I'] = [_]u8{0x3E,0x08,0x08,0x08,0x08,0x08,0x08,0x3E,0,0,0,0,0,0,0,0};
    font8x16['J'] = [_]u8{0x1E,0x02,0x02,0x02,0x02,0x42,0x42,0x3C,0,0,0,0,0,0,0,0};
    font8x16['K'] = [_]u8{0x42,0x44,0x48,0x70,0x48,0x44,0x42,0x42,0,0,0,0,0,0,0,0};
    font8x16['L'] = [_]u8{0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7E,0,0,0,0,0,0,0,0};
    font8x16['M'] = [_]u8{0x42,0x66,0x5A,0x42,0x42,0x42,0x42,0x42,0,0,0,0,0,0,0,0};
    font8x16['N'] = [_]u8{0x42,0x62,0x52,0x4A,0x46,0x42,0x42,0x42,0,0,0,0,0,0,0,0};
    font8x16['O'] = [_]u8{0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C,0,0,0,0,0,0,0,0};
    font8x16['P'] = [_]u8{0x7C,0x42,0x42,0x7C,0x40,0x40,0x40,0x40,0,0,0,0,0,0,0,0};
    font8x16['Q'] = [_]u8{0x3C,0x42,0x42,0x42,0x52,0x4A,0x46,0x3A,0,0,0,0,0,0,0,0};
    font8x16['R'] = [_]u8{0x7C,0x42,0x42,0x7C,0x48,0x44,0x42,0x42,0,0,0,0,0,0,0,0};
    font8x16['S'] = [_]u8{0x3C,0x42,0x40,0x3C,0x02,0x02,0x42,0x3C,0,0,0,0,0,0,0,0};
    font8x16['T'] = [_]u8{0x7F,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0,0,0,0,0,0,0,0};
    font8x16['U'] = [_]u8{0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x3C,0,0,0,0,0,0,0,0};
    font8x16['V'] = [_]u8{0x42,0x42,0x42,0x42,0x24,0x24,0x18,0x18,0,0,0,0,0,0,0,0};
    font8x16['W'] = [_]u8{0x42,0x42,0x42,0x5A,0x5A,0x66,0x66,0x42,0,0,0,0,0,0,0,0};
    font8x16['X'] = [_]u8{0x42,0x24,0x18,0x18,0x18,0x24,0x42,0x42,0,0,0,0,0,0,0,0};
    font8x16['Y'] = [_]u8{0x41,0x22,0x14,0x08,0x08,0x08,0x08,0x08,0,0,0,0,0,0,0,0};
    font8x16['Z'] = [_]u8{0x7E,0x02,0x04,0x18,0x20,0x40,0x40,0x7E,0,0,0,0,0,0,0,0};
    font8x16[' '] = [_]u8{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    font8x16['-'] = [_]u8{0,0,0,0,0x7E,0,0,0,0,0,0,0,0,0,0,0};
    font8x16['_'] = [_]u8{0,0,0,0,0,0,0,0,0,0,0,0,0x7E,0,0,0};
}